const express=require('express');
const mysql=require('mysql');
const urlobj=require('url');

const multer=require('multer');
//设置上传路径
var multerObj=multer({dest:'./www/img'});
const fs=require('fs');
const pathLib=require('path');

//引用自定义
const commte=require('../../../libs/common.js');
//链接数据库
var db=mysql.createPool({
    host:'localhost',
    user:'root',
    password:'',
    database:'leasedb'
});
//业务逻辑
module.exports = function() {
    var router = express.Router();

    router.get('/AddertiaryGoods.html',function (req,res,next) {
        //判断是否登陆
        if(req.session['userId']==null||req.session['userId']=="")
        {
            res.status(400).send('<script>alert("请登录");window.location.href="login.html";</script>').end();
        }
                //查询二级分类
        db.query("SELECT *FROM classa, classb WHERE classa.classAId = classb.classAId ORDER BY `classb`.`classBtime` DESC",function (err,data) {
            if(err)
            {
                res.status(500).send('<script>alert("出错了!");window.location.href="404.html";</script>').end();
            }else
            {
                if(data.length==0)
                {
                    res.status(500).send('<script>alert("没有数据哦!");window.location.href="index.html";</script>').end()

                }else
                {

                    req.oneortwo=data;
                    // console.log(req.oneortwo.length);
                    next();
                }

            }
        })
    });

    router.use('/AddertiaryGoods',function(req,res,next){

        console.log("原名文件"+req.files[0].originalname);
        //获得文件扩展名
        console.log("文件后缀"+pathLib.parse(req.files[0].originalname).ext);
        //文件全名
        var ext=pathLib.parse(req.files[0].originalname).ext;
        console.log("dxcvbnm"+ext);
        //文件旧存放路径
        var oldPath=req.files[0].path;
        // console.log("文件旧存放路径"+oldPath)
        //文件新存放路径
        var newPath=req.files[0].path+ext;
        console.log("文件新存放路径"+newPath)
        //文件新名
        var newFileName=req.files[0].filename+ext;



        fs.rename(oldPath,newPath,function(err){
            if(err)
            {
                res.status(500).send('upload is error');
            }
            else
            {
                db.query("INSERT INTO `classc`(`classCId`, `classBId`, `classCName`, `classCPIc`, `classCTitle`, `classCprice`, `classCDiscount`, `isActive`, `userId`, `deposit`, `ofBrowsing`, `state`, `leaseHMoney`, `leaseDMoney`, `leaseMMoney`, `classCTime`) VALUES (0,'"+ +req.body.classbID+"','"+req.body.classCName+"','"+ newFileName+"','"+ req.body.editorValue+"',"+ +req.body.tclassCprice+","+ +req.body.classCDiscount+","+ +req.body.isActive+",'"+ +req.session['userId']+"',0,0,0,0,0,0,'"+commte.getTime()+"')",function (err,data) {
                    if(err)
                    {
                        res.status(400).send('<script>alert("失败了!");</script>').end();
                    }else
                    {
                        res.status(400).send('<script>alert("添加成功");window.location.href="ertiaryGoods.html";</script>').end();
                        next();
                    }
                })

            }
        });


    });



    router.get('/AddertiaryGoods.html',function(req,res,next){
        // console.log("sdafasdfasdfasdfa"+req.oneortwo);
        res.render("admin/AddertiaryGoods.ejs",{secondwto:req.oneortwo});

});
    return router;
}